<?php

namespace app\admin\model;

use think\Model;

/**
 * Gifts
 */
class Gifts extends Model
{
    // 表名
    protected $name = 'gifts';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 字段类型转换
    protected $type = [
        'delete_time' => 'timestamp:Y-m-d H:i:s',
    ];

    protected static function onAfterInsert($model): void
    {
        if ($model->weigh == 0) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }

    public function countryCoinConf(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\common\model\country\CoinConf::class, 'country_coin_conf_id', 'id');
    }

    public function giftsTemplate(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\gifts\Template::class, 'gifts_template_id', 'id');
    }
}